package com.jrx.anytxn.atom.job.custBatch.config;

import com.jrx.anytxn.atom.bean.CustBatchSaveBean;
import com.jrx.anytxn.atom.config.AnytxnBatchProperties;
import com.jrx.anytxn.atom.job.custBatch.step.CustBatchProcessor;
import com.jrx.anytxn.atom.job.custBatch.step.CustBatchWriter;
import com.jrx.anytxn.atom.service.IDealProductSummaryService;
import com.jrx.anytxn.atom.service.impl.FastTaskExcutor;
import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;

@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class CustBatchConfiguration {

    Logger logger = LoggerFactory.getLogger(CustBatchConfiguration.class);

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    private CustBatchProcessor custBatchProcessor;
    @Autowired
    private CustBatchWriter custBatchWriter;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;
    @Autowired
    private IDealProductSummaryService dealProductSummaryService;

    @Bean
    public Job custBatchJob() {
        return jobBuilderFactory.get("custBatchJob")
                .start(custBatchStep())
                .next(productSummeryInsertStep())
                .build();
    }




    @Bean
    public Step custBatchStep() {
        return stepBuilderFactory.get("custBatchStep")
                .<CmCustomerFirstLevel, CustBatchSaveBean>chunk(anytxnBatchProperties.getCustBatch().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(custBatchReader())
                .processor(custBatchProcessor)
                .writer(custBatchWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new FastTaskExcutor())
                .throttleLimit(anytxnBatchProperties.getCustBatch().getThrottleLimit())//多线程
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<CmCustomerFirstLevel> custBatchReader() {
        //获取需要催收的产品
        MyBatisCursorItemReader<CmCustomerFirstLevel> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        cursorItemReader.setQueryId("com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerFirstLevelMapper.selectAllCust");
        SynchronizedItemStreamReader<CmCustomerFirstLevel> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }

    @Bean
    public Step productSummeryInsertStep() {
        return stepBuilderFactory.get("productSummeryInsertStep")
                .tasklet(productSummeryTasklet())
                .build();
    }

    @Bean
    public Tasklet productSummeryTasklet() {
        return (stepContribution, chunkContext) -> {
            dealProductSummaryService.writeProductSummary();
            return RepeatStatus.FINISHED;
        };
    }

}
